package web.Servlet;

import com.alibaba.fastjson.JSON;
import domain.news;
import service.Serviceimpl.newsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "mhnewsServlet" ,urlPatterns = "/mohu")
public class mhnewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String titlecx = req.getParameter("titlecx");
        newsService deleService = new newsService();
        try {
            titlecx+="%";
            System.out.println(titlecx);
            List<news> cx = deleService.cx(titlecx);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
            for(int i=0;i<cx.size();i++){
                Date create_time = cx.get(i).getCreate_time();
                String format = simpleDateFormat.format(create_time);
                cx.get(i).setTime(format);
            }

            String s = JSON.toJSONString(cx);
            resp.getWriter().print(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
